package com.xzy.servlet;

import com.xzy.domain.Order;
import com.xzy.domain.OrderItem;
import com.xzy.domain.User;
import com.xzy.service.IBookCart;
import com.xzy.service.IOrderItemService;
import com.xzy.service.IOrderService;
import com.xzy.service.IUserService;
import com.xzy.service.impl.BookCartImpl;
import com.xzy.service.impl.OrderItemService;
import com.xzy.service.impl.OrderServiceImpl;
import com.xzy.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/OrderServlet")
public class OrderServlet extends BaseServlet {
    IOrderService Orderservice = new OrderServiceImpl();
    IUserService userService = new UserServiceImpl();
    IBookCart bookCartservice = new BookCartImpl();
    IOrderItemService orderItemService = new OrderItemService();

    /**
     * 该方法执行流程(数据库方面):
     *
     * 因为该方法使用了不止一个的dao,所以存在事务操作
     * 第一个dao从连接池获取conn连接后不直接关闭,而是存入ThreadLoca线程共享对象中
     * 后续dao从ThreadLoca中获取conn连接(此时所有dao用的都是同一个连接)
     * 所有dao操作完成后,若无异常出现则提交并关闭连接(同时移除ThreadLoca中数据)
     * 若有异常则回滚并关闭(同时移除ThreadLoca中数据)
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        IBookCart cart = (IBookCart) req.getSession().getAttribute("cart");
        String username = (String) req.getSession().getAttribute("username");
        User user = userService.queryByuserName(username);


        String orderid = null;
        //将中连接到此处关闭,因为该service不只调用了一个dao(有事务存在,所以要在此时统一关闭连接并提交数据)
        //为了保证service中不同的dao使用相同的connection,在获取连接时使用了ThreadLocal共享同一线程中的连接对象


            orderid = Orderservice.settlement(cart.getCart(), user.getId());

            req.getSession().setAttribute("orderid",orderid);

            resp.sendRedirect(req.getContextPath()+"/pages/cart/checkout.jsp");

            bookCartservice.clean();




    }

    protected void ListOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String username = (String) req.getSession().getAttribute("username");
        User user = userService.queryByuserName(username);
        List<Order> orders = Orderservice.queryById(user.getId());
        req.setAttribute("orders",orders);

        req.getRequestDispatcher("/pages/order/order.jsp").forward(req,resp);
    }
    protected void ListOrderItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String orderid = (String) req.getParameter("orderid");
        List<OrderItem> orderItems = orderItemService.queryByOrderId(orderid);
        String path = (String) req.getParameter("path");
        System.out.println(orderid);
        System.out.println(orderItems);
        req.setAttribute("orderItems",orderItems);

        req.getRequestDispatcher(path).forward(req,resp);
    }
    protected void ListAllOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        List<Order> query = Orderservice.query();
        req.setAttribute("orderList",query);


        req.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(req,resp);
    }

    protected void updataOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        String orderid = req.getParameter("orderid");

        Orderservice.updataStatus(orderid,1);


        req.getRequestDispatcher("OrderServlet?type=ListAllOrder").forward(req,resp);

    }
}
